//
//  WPMenu.m
//  Weibo
//
//  Created by wpapple on 16/5/31.
//  Copyright © 2016年 wpapple. All rights reserved.
//

#import "WPMenu.h"

static UIButton * _cover;

static dismissBlock _block;

@implementation WPMenu

+(void)show:(UIView * )contentView withView:(UIView *)inVIew
{
    //    //弹出菜单
    UIWindow *_window = [UIApplication sharedApplication].keyWindow;
    [_window setBackgroundColor:[UIColor blackColor]];
    _window.hidden = NO;
    
    UIButton *coverButton = [[UIButton alloc]init];
    coverButton.frame = [UIScreen mainScreen].bounds;
    [coverButton setBackgroundColor:[UIColor clearColor]];
    //    coverButton.alpha = 0.5;
    
    [coverButton addTarget:self action:@selector(coverButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [_window addSubview:coverButton];
    _cover = coverButton;
    
    //创建菜单
    UIImageView *menuImage = [[UIImageView alloc]init];
    menuImage.userInteractionEnabled = YES;
    
    UIImage *image = [UIImage imageNamed:@"popover_background"];
    menuImage.image = image;
    [coverButton addSubview:menuImage];
    
    
    //添加内容到菜单上
    [menuImage addSubview:contentView];
    
    //设置菜单的frame
    CGFloat menuW = CGRectGetMaxX(contentView.frame) + contentView.x;
    CGFloat menuH = CGRectGetMaxY(contentView.frame) + contentView.y;
    
    menuImage.frame = CGRectMake(0, 0, menuW, menuH);
    
    //转换坐标
    CGRect resultFrame = [_window convertRect:inVIew.frame fromView:inVIew.superview];
    
    //设置菜单的Y值
    menuImage.y = CGRectGetMaxY(resultFrame);
//        menuImage.centerx = resultFrame.origin.x + (resultFrame.size.width * 0.5);
    menuImage.x = 80;

}

+(void)coverButtonClicked:(UIButton *)view
{
    [_cover removeFromSuperview];
    
    if (_block != nil) {
        _block();
        _block = nil;
    }
}

+(void)show:(UIView * )contentView withView:(UIView *)inVIew dismiss:(dismissBlock)dismiss
{
    _block = [dismiss copy];
    
    [self show:contentView withView:inVIew];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
